package framework
{
	import flash.utils.Dictionary;
	
	import mx.core.mx_internal;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	use namespace mx_internal;

	public class BranchResponder implements IResponder
	{
		public var token:AsyncToken;
		public var branch:Array;
		public var results:Dictionary;
		public function BranchResponder()
		{
			token = new AsyncToken(null);
			results = new Dictionary;
		}

		public function result(data:Object):void
		{
			var e:ResultEvent = ResultEvent(data);
			results[e.token] = e.result;
			branch.splice(branch.indexOf(e.token),1);
			if (!branch.length)
			{
				var re:ResultEvent = ResultEvent.createEvent(results,token);
				re.mx_internal::callTokenResponders();
			}
		}
		
		public function fault(info:Object):void
		{
			var fe:FaultEvent = FaultEvent(info);
			token.mx_internal::applyFault(fe);
		}
		
	}
}